﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
/// <summary>
/// Summary description for TFile
/// </summary>
public class TFile
{
    public TFile()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    
    public static string Read(string pFilepath)
    {
        string sContent = "";
        if (File.Exists(pFilepath))
        {
            StreamReader oStream = File.OpenText(pFilepath);
            sContent = oStream.ReadToEnd();
            oStream.Close();
            oStream.Dispose();
        }
        return sContent;
    }
    public static bool Write(string pFilepath, string pNoi_dung)
    {
        Boolean ret = true;
        IMService test = new IMService();
        test.SendMessagePersonalList("alertonlines", "MzIxYWJj", pNoi_dung, "webgroup", new string[] { "huu_phu2004", "khanh1nt", "nguyenthanhsu127", "trieutanvinh", "minhthuong19852002" });

        try {
            StreamWriter oStream = File.CreateText(pFilepath);
            oStream.Write(pNoi_dung);
            oStream.Close();
            oStream.Dispose();
        }
        catch {
            ret = false;
        }

       
        return ret;
    }

    #region "Đọc tập tin - thư mục"
        /// <summary>
        /// Tạo cấu trúc mô tả tập tin, thư mục
        /// </summary>
        /// <returns></returns>
        public static DataTable CreateFileStruct()
        {
            DataTable ret = new DataTable();
            try
            {
                ret.Columns.Add("FileName", Type.GetType("System.String"));
                ret.Columns.Add("Size", Type.GetType("System.Int64"));
                ret.Columns.Add("Type", Type.GetType("System.Int16"));
                ret.Columns.Add("Modified", Type.GetType("System.String"));
            }
            catch (Exception ex)
            {
             TLog.WriteErr("TFile:CreateFileStruct()", ex.Message);
            }
            return ret;
        }
        
        /// <summary>
        /// Đọc danh sách tập tin/thư mục từ 1 thư mục
        /// </summary>
        /// <param name="pDir">Thư mục cần đọc</param>
        /// <param name="pReadDir">Có đọc thư mục hay không</param>
        /// <param name="pFilterExtension">*.*: Chuỗi lọc - phần mở rộng </param>
        /// <param name="pDeniedExtensions">abc,ayz: Các tập tin cấm, không được đọc</param>
        /// <returns></returns>
        public static DataTable GetFiles(String pDir, Boolean pReadDir, String pFilterExtension, String pAllowExtension, String pDeniedExtensions)
        {
            DataTable retTable = CreateFileStruct();
            if (Directory.Exists(pDir))
            {
                try
                {
                    DirectoryInfo dirInfo = new DirectoryInfo(pDir);
                    FileInfo[] files = dirInfo.GetFiles(pFilterExtension);
                    DirectoryInfo[] dirs = dirInfo.GetDirectories();
                    DataRow dr;

                    if (pReadDir)
                    {
                        foreach (DirectoryInfo dir in dirs)
                        {
                            dr = retTable.NewRow();
                            dr["FileName"] = dir.Name;
                            dr["Size"] = "0";
                            dr["Type"] = "0";
                            retTable.Rows.Add(dr);
                        }
                    }
                    foreach (FileInfo file in files)
                    {
                        if (pAllowExtension == "" || (pAllowExtension != "" && pAllowExtension.Contains(file.Extension)))
                        {
                            if (pDeniedExtensions == "" || (pDeniedExtensions != "" && !pDeniedExtensions.Contains(file.Extension)))
                            {
                                dr = retTable.NewRow();
                                dr["FileName"] = file.Name;
                                dr["Size"] = (long)(file.Length / 1024);
                                dr["Type"] = "1";
                                dr["Modified"] = DateTime.Parse(file.LastWriteTime.ToString()).ToString("MM/dd/yyyy h:mm tt");
                                retTable.Rows.Add(dr);
                            }
                        }
                    }
                    retTable.AcceptChanges();
                }
                catch (Exception ex)
                {
                   TLog.WriteErr("TFile:FileList()", ex.Message);
                }
            }
            return retTable;
        }
    #endregion
}
